package com.sugar.global;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.HashMap;
import java.util.Map;

/**
 * @author felix
 * @date 2024/11/15 15:15
 */
@ControllerAdvice
public class MyGlobalHandler {

    /** 方式一 **/
    @ModelAttribute
    public void presetParam(Model model){
        model.addAttribute("globalAttr","this is a global param");
    }

    /** 方式二 **/
    @ModelAttribute()
    public Map<String, String> presetParam(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "map默认获取方式是modelMap.get(\"map\")");
        map.put("key2", "value2");
        map.put("key3", "value3");
        return map;
    }

    /** 方式三 **/
    @ModelAttribute("myMap")
    public Map<String, String> presetParam1(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("key1", "map自定义获取方式是modelMap.get(\"myMap\")");
        map.put("key2", "value2");
        map.put("key3", "value3");
        return map;
    }
}
